package com.ucomm.talent.manager;


import org.apache.http.client.HttpResponseException;
import org.json.JSONObject;

import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.model.HotCompanyDetailsInfo;
import com.ucomm.talent.model.JobFairDetailsInfo;
import com.ucomm.talent.model.JobOffersDetailsInfo;
import com.ucomm.talent.model.NewsDetailsInfo;
import com.ucomm.talent.model.RecommendJobDetailInfo;
import com.ucomm.talent.model.RecommendedJobsInfo;
import com.ucomm.talent.model.ResumeDetailsInfo;
import com.ucomm.talent.model.TalentsDetailsInfo;
import com.vita.bduck.BDuckHttp;
import com.vita.bduck.http.AjaxCallBack;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class DetailSearchManager {
	
    private Context mContext;
    private Handler mHander;
	public DetailSearchManager(Context mContext, Handler mHander) {
		this.mContext = mContext;
		this.mHander = mHander;
	}
    
    /**
     * 从网络搜索信息
     */
    @SuppressWarnings("unchecked")
	public void searchDetailDataFromNetwork(final String url, final Object... params){
        BDuckHttp fh = new BDuckHttp();

        fh.get(url, new AjaxCallBack() {
			 @Override
            public void onLoading(long count, long current) { //每1秒钟自动被回调一次
                // textView.setText(current+"/"+count);
                Log.i("vita", ">>>>>>>>..onLoading..<<<<<<<<");
            }

            @Override
            public void onStart() {
                //开始http请求的时候回调
                Log.i("vita", ">>>>>>>>..start..<<<<<<<<");
                //UToast.makeText(mContext, "开始加载", UToast.UToast_SHORT).show();
                Message msg = new Message();
                msg.what = AppConstants.MsgStatus.MSG_LOADING;
                mHander.sendMessage(msg);
            }

            @Override
            public void onFailure(Throwable t, String strMsg) {
                //加载失败的时候回调
                Log.i("vita", ">>>>>>>>....<<<<<<<<"
                        + strMsg);
                Message msg = new Message();
                msg.what = AppConstants.MsgStatus.MSG_ERROR;
                if(t instanceof  HttpResponseException){
                    msg.obj = AppException.http((HttpResponseException)t);
                    mHander.sendMessage(msg);
                }else{
                    msg.obj = AppException.http(new Exception("strMsg"));
                    mHander.sendMessage(msg);
                }

            }

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				if(t instanceof String){
					String data = (String) t;
					if(url.contains(AppConstants.JOB_OFFERS_DETAILS)){
						Message msg = new Message();
						try {
							JobOffersDetailsInfo details = resolveJobOffersDetailsJson(data);
							msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
							msg.obj = details;
							mHander.sendMessage(msg);
						} catch (AppException e) {
							e.printStackTrace();
                            msg.what = -1;
                            msg.obj = e;
                            mHander.sendMessage(msg);
						}
					}else if(url.contains(AppConstants.HOT_COMPANY_DETAILS)){
						Message msg = new Message();
						try {
							HotCompanyDetailsInfo details = resolveHotComInfoJson(data);
							msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
							msg.obj = details;
							mHander.sendMessage(msg);
						} catch (AppException e) {
							e.printStackTrace();
                            msg.what = -1;
                            msg.obj = e;
                            mHander.sendMessage(msg);
						}
					}else if(url.contains(AppConstants.JOB_FAIR_DETAILS)){
						Message msg = new Message();
						try {
							JobFairDetailsInfo details = resolveJobFairInfoJson(data);
							msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
							msg.obj = details;
							mHander.sendMessage(msg);
						} catch (AppException e) {
							e.printStackTrace();
                            msg.what = -1;
                            msg.obj = e;
                            mHander.sendMessage(msg);
						}
					}else if(url.contains(AppConstants.TALENTS_DETAILS)){
						Message msg = new Message();
						try {
							TalentsDetailsInfo details = resolveTalentsDetailsInfo(data);
							msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
							msg.obj = details;
							mHander.sendMessage(msg);
						} catch (AppException e) {
							e.printStackTrace();
                            msg.what = -1;
                            msg.obj = e;
                            mHander.sendMessage(msg);
						}
					}else if(url.contains(AppConstants.JOB_RESUME_DETAILS)){
						Message msg = new Message();
						try {
							ResumeDetailsInfo details = resolveResumeDetailsInfo(data);
							msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
							msg.obj = details;
							mHander.sendMessage(msg);
						} catch (AppException e) {
							e.printStackTrace();
                            msg.what = -1;
                            msg.obj = e;
                            mHander.sendMessage(msg);
						}
					}else if(url.contains(AppConstants.NEWS_DETAILS)){
						Message msg = new Message();
						try {
							NewsDetailsInfo details = resolveNewsDetailsInfo(data);
							msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
							msg.obj = details;
							mHander.sendMessage(msg);
						} catch (AppException e) {
							e.printStackTrace();
                            msg.what = -1;
                            msg.obj = e;
                            mHander.sendMessage(msg);
						}
					}else if(url.contains(AppConstants.RECOMMENDED_JOBS_DETAILS)){
						Message msg = new Message();
						try {
							RecommendJobDetailInfo details = resolveRecDetailsInfo(data);
							msg.what = AppConstants.MsgStatus.MSG_SUCCUSS;
							msg.obj = details;
							mHander.sendMessage(msg);
						} catch (AppException e) {
							e.printStackTrace();
                            msg.what = -1;
                            msg.obj = e;
                            mHander.sendMessage(msg);
						}
					}
				}
			}
        });
    }
    //推荐职位详细信息
    private RecommendJobDetailInfo resolveRecDetailsInfo(String data) throws AppException{
    	RecommendJobDetailInfo details = new RecommendJobDetailInfo();
    	try {
			JSONObject jsonObject = new JSONObject(data);
			details.setJobName(jsonObject.getString("name"));
			details.setSalaryRange(jsonObject.getString("salary"));
			details.setWorkerNum(jsonObject.getString("number"));
			details.setWorkAge(jsonObject.getString("exp"));
			details.setDescription(jsonObject.getString("description"));
			details.setProvinceId(jsonObject.getString("provinceid"));
			details.setCityId(jsonObject.getString("cityid"));
		} catch (Exception e) {
			e.printStackTrace();
			throw AppException.http(e);
		}
    	return details;
    }
    //行业资讯详细信息
    private NewsDetailsInfo resolveNewsDetailsInfo(String data) throws AppException{
    	NewsDetailsInfo details = new NewsDetailsInfo();
    	try {
			JSONObject jsonObject = new JSONObject(data);
			details.setAuthor(jsonObject.getString("author"));
			details.setDatetime(jsonObject.getString("datetime"));
			details.setDescription(jsonObject.getString("description"));
			details.setKeyword(jsonObject.getString("keyword"));
			details.setTitle(jsonObject.getString("title"));
		} catch (Exception e) {
			e.printStackTrace();
			throw AppException.http(e);
		}
    	return details;
    }
    
    //应聘简历详细信息
    private ResumeDetailsInfo resolveResumeDetailsInfo(String data) throws AppException{
    	ResumeDetailsInfo details = new ResumeDetailsInfo();
    	try {
			JSONObject jsonObject = new JSONObject(data);
			JSONObject resume = jsonObject.getJSONObject("resume");
			details.setCityId(resume.getString("cityid"));
			details.setJobSon(resume.getString("job1_son"));
			details.setJobPost(resume.getString("job_post"));
			details.setName(resume.getString("name"));
			details.setProvinceId(resume.getString("provinceid"));
			details.setReport(resume.getString("report"));
			details.setType(resume.getString("type"));
		} catch (Exception e) {
			e.printStackTrace();
			throw AppException.http(e);
		}
    	return details;
    }
    //高级人才详细信息
    private TalentsDetailsInfo resolveTalentsDetailsInfo (String data) throws AppException{
    	TalentsDetailsInfo details = new TalentsDetailsInfo();
    	try {
			JSONObject jsonObject = new JSONObject(data);
			JSONObject resume = jsonObject.getJSONObject("resume");
			details.setCityId(resume.getString("cityid"));
			details.setJob1_son(resume.getString("job1_son"));
			details.setJob_post(resume.getString("job_post"));
			details.setName(resume.getString("name"));
			details.setProvinceId(resume.getString("provinceid"));
			details.setReport(resume.getString("report"));
			details.setType(resume.getString("type"));
		} catch (Exception e) {
			e.printStackTrace();
			throw AppException.http(e);
		}
    	return details;
    }
    //招聘现场详细信息
    private JobFairDetailsInfo resolveJobFairInfoJson (String data) throws AppException{
    	JobFairDetailsInfo details = new JobFairDetailsInfo();
    	try {
			JSONObject jsonObject = new JSONObject(data);
			details.setTraffic(jsonObject.getString("traffic"));
			details.setFairDescription(jsonObject.getString("body"));
		} catch (Exception e) {
			e.printStackTrace();
			throw AppException.http(e);
		}
    	return details;
    }
    //热门企业详细信息
    private HotCompanyDetailsInfo resolveHotComInfoJson(String data) throws AppException{
    	HotCompanyDetailsInfo details = new HotCompanyDetailsInfo();
    	try {
			JSONObject jsonObject = new JSONObject(data);
			details.setCompanyPro(jsonObject.getString("pr"));
			details.setProvinceId(jsonObject.getString("provinceid"));
			details.setCityId(jsonObject.getString("cityid"));
			details.setHy(jsonObject.getString("hy"));
			details.setDescription(jsonObject.getString("content"));
			details.setMoney(jsonObject.getString("money"));
			details.setMun(jsonObject.getString("mun"));
			details.setsDate(jsonObject.getString("sdate"));
		} catch (Exception e) {
			e.printStackTrace();
			throw AppException.http(e);
		}
    	return details;
    }
    //招聘信息详细信息
    private JobOffersDetailsInfo resolveJobOffersDetailsJson(String data) throws AppException{
    	JobOffersDetailsInfo details = new JobOffersDetailsInfo();
    	try {
			JSONObject jsonObject = new JSONObject(data);
			details.setJobName(jsonObject.getString("name"));
			details.setSalaryRange(jsonObject.getString("salary"));
			details.setWorkerNum(jsonObject.getString("number"));
			details.setWorkAge(jsonObject.getString("exp"));
			details.setDescription(jsonObject.getString("description"));
			details.setProvinceId(jsonObject.getString("provinceid"));
			details.setCityId(jsonObject.getString("cityid"));
		} catch (Exception e) {
			e.printStackTrace();
			throw AppException.http(e);
		}
    	return details;
    }
}
